package com.xianzhiOA.controller;/**
 * Created by Administrator on 2017/5/9.
 */

import com.xianzhiOA.bean.*;
import com.xianzhiOA.service.XzEmpService;
import com.xianzhiOA.service.XzProjectService;
import com.xianzhiOA.service.XzWorkService;
import com.xianzhiOA.service.XzWorkingService;
import com.xianzhiOA.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 工作量的控制层
 *
 * @author 甘汝雷
 * @create 2017-05-09 17:15
 **/
@Controller
@RequestMapping("Working")
@SessionAttributes("userLogin")
public class XzWorkingController {
    @Autowired
    private XzWorkingService workingService;
    @Autowired
    private XzEmpService empService;
    @Autowired
    private XzWorkService workService;
    @Autowired
    private XzProjectService projectService;
    //添加
    @RequestMapping("addWorking.emp")
    public ModelAndView addWorking(XzWorking working) {
        ModelAndView mv = errors(working,0);
        if (mv.getViewName().equals("backEnd/workingEdit"))return mv;
        workingService.insert(working);
        return mv;
    }

    //验证表单
    public ModelAndView errors(XzWorking working,int i) {
        ModelAndView mv = new ModelAndView("redirect:selWorkingAll.emp");
        Map map = new HashMap();
        if (working.getWorkingTime() == null) map.put("workingTime", "周期不能为空");
        if(working.getWorkingTime() != null){
            Map byDate = workService.getWeekByDate(working.getWorkingTime());
            Map map1 = new HashMap();
            map1.put("begin2",byDate.get("begin"));
            map1.put("end2",byDate.get("end"));
            map1.put("workingProject",working.getWorkingProject());
            map1.put("isdelete",0);
            List<XzWorking> workingList = workingService.selAll(map1);
            if (!workingList.isEmpty()){
                if (i==1){
                    for (XzWorking w:workingList) {
                        if (w.getWorkingId()!=working.getWorkingId())map.put("workingTime","该归属项目已存在当前周期的工作量分布信息");
                    }
                }
                if (i==0) map.put("workingTime","该归属项目已存在当前周期的工作量分布信息");
            }
        }
        if (map.size() > 0) {
            working.setWorkingTime(new Date());
            mv.addObject("errors", map);
            mv.addObject("working", working);
            mv.addObject("projectList", projectService.selAll(new XzProject()));
            mv.setViewName("backEnd/workingEdit");
        }
        return mv;
    }

    //删除
    @RequestMapping("deleteWorking.emp")
    public ModelAndView deleteWorking(Long workingId) {
        ModelAndView mv = new ModelAndView("redirect:selWorkingAll.emp");
        workingService.isdelete(workingId);
        return mv;
    }

    //修改
    @RequestMapping("updateWorking.emp")
    public ModelAndView update(XzWorking working) {
        ModelAndView mv = errors(working,1);
        if (mv.getViewName().equals("backEnd/workingEdit"))return mv;
        workingService.update(working);
        return mv;
    }

    //编辑
    @RequestMapping("workingEdit.emp")
    public ModelAndView workingEdit(long workingId,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/workingEdit");
        XzWorking working = new XzWorking();
        if (workingId != 0) {
            working = workingService.selById(workingId);
        }else {
            working.setEmpId(emp.getEmpId());
            working.setWorkingTime(new Date());
        }
        Map work = getWork(working.getEmpId(), working.getWorkingTime());
        mv.addObject("planTime",work.get("plan"));
        mv.addObject("actualTime",work.get("actual"));
        mv.addObject("working", working);
        mv.addObject("projectList",projectService.selAll(new XzProject()));
        return mv;
    }
    @ResponseBody
    @RequestMapping("getWork.emp")
    //通过员工id和date获取员工的周计划工作量和实际工作量
    public Map getWork(Long empId,Date workingTime){
        XzWork work = new XzWork();
        Map map = new HashMap();
        work.setEmpId(empId);
        work.setWorkWeekTime(workingTime);
        List<XzWork> works = workService.selAll(work);
        int i = 0,j = 0;//一周的计划时间、一周的实际时间。
        int p1=0,p2=0,a1=0,a2=0;//计划比例、计划工作量、实际比例、实际工作量。
        for (XzWork w:works) {
            i +=(int)w.getWorkPlan();
            j += (int)w.getWorkActual();
        }
        map.put("plan",i);
        map.put("actual",j);
        return map;
    }

    //分页查询
    @RequestMapping("selWorkingAll.emp")
    public ModelAndView selWorkingAll(XzWorking working, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "8") int rows,
                                      Date begin1, Date end1, Date begin2, Date end2,@ModelAttribute("userLogin")XzEmp emp) {
        ModelAndView mv = new ModelAndView("backEnd/workingShow");
        if (working.getEmpId()==null&&working.getEmpName()==null)working.setEmpId(emp.getEmpId());
        PageBean<XzWorking> pageBean = workingService.selAll(working, page, rows, begin1, end1, begin2, end2);
        mv.addObject("workingList", myWorking(pageBean.getList()));
        mv.addObject("working", working);
        mv.addObject("page", pageBean.getPageNum());
        mv.addObject("pages", pageBean.getPages());
        mv.addObject("rows", pageBean.getPageSize());
        mv.addObject("total", pageBean.getTotal());
        mv.addObject("begin1", begin1);
        mv.addObject("end1", end1);
        mv.addObject("begin2", begin2);
        mv.addObject("end2", end2);
        return mv;
    }

    //通过工作量分布周期，得到该工作量当周的开始时间和结束时间
    public List<XzWorking> myWorking(List<XzWorking> workings){
        for (XzWorking w : workings) {
            Map byDate = workService.getWeekByDate(w.getWorkingTime());
            w.setWorkingTimeBegin((String)byDate.get("begin"));
            w.setWorkingTimeEnd((String)byDate.get("end"));
            if (w.getWorkingActual()!=0&&w.getWorkingPlan()!=0)w.setBias((w.getWorkingActual()-w.getWorkingPlan())*100/w.getWorkingPlan());
        }
        return workings;
    }

    //计算偏差率
    public List<XzPhase> bias(List<XzPhase> phaseList){
        for (XzPhase p:phaseList) {
            if (p.getPhaseActual()!=0&&p.getPhasePlan()!=0) {
                p.setBias((p.getPhaseActual() - p.getPhasePlan()) * 100 / p.getPhasePlan());
            }else p.setBias(0);
        }
        return phaseList;
    }
    //通过id查询
    @RequestMapping("selById.emp")
    public ModelAndView selById(Long workingId) {
        ModelAndView mv = new ModelAndView("backEnd/workingDetails");
        XzWorking working = workingService.selById(workingId);
        working.setPhaseList(bias(working.getPhaseList()));
        Map byDate = workService.getWeekByDate(working.getWorkingTime());//获取该date当周的开始时间和结束时间
        working.setWorkingTimeBegin((String)byDate.get("begin"));
        working.setWorkingTimeEnd((String)byDate.get("end"));
        if (working.getWorkingActual()!=0&&working.getWorkingPlan()!=0)working.setBias((working.getWorkingActual()-working.getWorkingPlan())*100/working.getWorkingPlan());
        mv.addObject("working",working);
        return mv;
    }
}
